package com.drinks.yiniang.adapter.user.order;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.drinks.yiniang.R;
import com.drinks.yiniang.adapter.base.BaseQuickRCVAdapter;
import com.drinks.yiniang.adapter.listview.UploadImageGridAdapter;
import com.drinks.yiniang.entity.common.UploadImageJson;
import com.drinks.yiniang.entity.user.order.OrderEvaluateJson;
import com.drinks.yiniang.ui.widget.layout.DimensionWrapLayout;
import com.drinks.yiniang.ui.widget.view.RadarMapView;
import com.drinks.yiniang.utils.GlideUtils;
import com.hansen.library.listener.OnListItemClickListener;
import com.hansen.library.ui.widget.image.RatioRoundImageView;
import com.hansen.library.ui.widget.listview.NoScrollGridView;
import com.hansen.library.ui.widget.ratingbar.CustomRatingBar;
import com.hansen.library.utils.CommonUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 待评价商品列表adapter
 * @author han
 * @date 2019/8/30
 */
public class OrderEvaluate2Adapter extends RecyclerView.Adapter {

    private Context mContext;

    private UploadImageGridAdapter mUploadImageGridAdapter;

    private List<OrderEvaluateJson.OrderEvaluateGoods> mList;
    private List<UploadImageJson.UploadImageData> mImageList;
    //维度数组
    private List<OrderEvaluateJson.OrderEvaluateRadar> mRadars;
    private OrderEvaluateJson.OrderEvaluateGoods mItemData;

    //edittext里的文字内容集合
    private SparseArray<String> mSparseArray = new SparseArray();

    private OnListItemClickListener onListItemClickListener;
    private CustomRatingBar.OnRatingChangeListener onRatingChangeListener;
    private TextWatcher mWatcher;

    //当前用户是否专家评论
    private boolean isSpecialistComment;


    public OrderEvaluate2Adapter(Context mContext, List<OrderEvaluateJson.OrderEvaluateGoods> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int position) {
        View mView = LayoutInflater.from(mContext).inflate(R.layout.item_order_evaluate, viewGroup, false);

        ViewHolder holder = new ViewHolder(mView);

        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int position) {
        final ViewHolder mHolder = (ViewHolder) viewHolder;

        mItemData = mList.get(position);

        //商品图
        GlideUtils.loadSquareImage(mContext, mHolder.iv_order_evaluate_cover, mItemData.getCover_url());
        //商品名称
        mHolder.tv_order_evaluate_name.setText(mItemData.getGoods_name());
        //推荐语
        mHolder.tv_order_evaluate_slogan.setText(mItemData.getRecommend_reason());
        //评价语
        setEvaluateContent(mHolder);

        //评分
        setGoodsRatingBar(mHolder);

        //是否专家评论
        if(isSpecialistComment) {
            mHolder.ll_order_evaluate_dimension.setVisibility(View.VISIBLE);
            mHolder.v_order_evaluate_radar_map.setVisibility(View.VISIBLE);
        } else {
            mHolder.v_order_evaluate_radar_map.setVisibility(View.GONE);
            mHolder.ll_order_evaluate_dimension.setVisibility(View.GONE);
        }

        //图片
        mImageList = new ArrayList<>();

        mUploadImageGridAdapter = new UploadImageGridAdapter(mContext, mImageList);

        mUploadImageGridAdapter.setOnListItemClickListener(new OnListItemClickListener() {
            @Override
            public void onItemClick(View mView, int position) {

            }
        });

        mHolder.gv_order_evaluate_photo.setAdapter(mUploadImageGridAdapter);
    }

    @Override
    public int getItemCount() {
        return CommonUtils.getListSize(mList);
    }

    private void setEvaluateContent(final ViewHolder viewHolder) {
        mWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                //每次修改文字后，保存在数据集合中
                mList.get(viewHolder.getAdapterPosition()).setCommentContent(editable.toString());
            }
        };

        if(viewHolder.et_order_evaluate_content.getTag() instanceof TextWatcher) {
            viewHolder.et_order_evaluate_content.removeTextChangedListener(mWatcher);
        }

        viewHolder.et_order_evaluate_content.setText(mList.get(viewHolder.getAdapterPosition()).getCommentContent());
        viewHolder.et_order_evaluate_content.setTag(mWatcher);
        viewHolder.et_order_evaluate_content.addTextChangedListener(mWatcher);
    }

    //商品评分
    private void setGoodsRatingBar(final ViewHolder viewHolder) {
        onRatingChangeListener = new CustomRatingBar.OnRatingChangeListener() {
            @Override
            public void onRatingChange(CustomRatingBar ratingBar, float ratingCount) {
                mList.get(viewHolder.getAdapterPosition()).setGoodsScore(ratingCount);
            }
        };

        if(viewHolder.rating_order_evaluate.getTag() instanceof CustomRatingBar.OnRatingChangeListener) {
            viewHolder.rating_order_evaluate.setOnRatingChangeListener(null);
        }

        viewHolder.rating_order_evaluate.setStar(mList.get(viewHolder.getAdapterPosition()).getGoodsScore());
        viewHolder.rating_order_evaluate.setOnRatingChangeListener(onRatingChangeListener);
        viewHolder.rating_order_evaluate.setTag(onRatingChangeListener);
    }

    public void setSpecialistComment(boolean specialistComment) {
        isSpecialistComment = specialistComment;
    }

    public boolean isSpecialistComment() {
        return isSpecialistComment;
    }

    public List<OrderEvaluateJson.OrderEvaluateRadar> getRadars() {
        return mRadars;
    }

    public void setRadars(List<OrderEvaluateJson.OrderEvaluateRadar> mRadars) {
        this.mRadars = mRadars;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        //封面
        RatioRoundImageView iv_order_evaluate_cover;
        //名称
        TextView tv_order_evaluate_name;
        //推荐语
        TextView tv_order_evaluate_slogan;
        //评价内容
        EditText et_order_evaluate_content;
        NoScrollGridView gv_order_evaluate_photo;
        //商品评分
        CustomRatingBar rating_order_evaluate;
        //雷达图
        RadarMapView v_order_evaluate_radar_map;
        //多维度评价
        DimensionWrapLayout ll_order_evaluate_dimension;

        int position;

        public ViewHolder(View itemView) {
            super(itemView);

            iv_order_evaluate_cover = itemView.findViewById(R.id.iv_order_evaluate_cover);

            tv_order_evaluate_name = itemView.findViewById(R.id.tv_order_evaluate_name);
            tv_order_evaluate_slogan = itemView.findViewById(R.id.tv_order_evaluate_slogan);

            et_order_evaluate_content = itemView.findViewById(R.id.et_order_evaluate_content);

            gv_order_evaluate_photo = itemView.findViewById(R.id.gv_order_evaluate_photo);

            rating_order_evaluate = itemView.findViewById(R.id.rating_order_evaluate);

            v_order_evaluate_radar_map = itemView.findViewById(R.id.v_order_evaluate_radar_map);

            ll_order_evaluate_dimension = itemView.findViewById(R.id.ll_order_evaluate_dimension);
        }
    }
}
